﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_basisset_GroupPermision : AdminPageBase
{
    int GroupId;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        // GetUrlArgs();
        GetUserPermisions();
        GetGroupId();
        if (!IsPostBack)
        {
           
            BindGroupPermision();
        }

    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }


    }
    protected void GetGroupId()
    {
        if (Request.QueryString["id"] != null)
        {
            GroupId = Convert.ToInt32(Request.QueryString["id"]);

        }
    }
    protected void BindGroupPermision()
    {
        DataTable ds = jtDataAccess.GetGroupPermision(GroupId);
        if (ds != null && ds.Rows.Count > 0)
        {

            grdvPermision.DataSource = ds;
            grdvPermision.DataBind();
        }
    }
   
    protected void grdvPermision_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }
    protected void grdvPermision_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DataSet ds = new DataSet();
        ds = jtDataAccess.GetList("Permision", "1=1", "PermisionId");
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            CheckBoxList ckblp = (CheckBoxList)e.Row.FindControl("ckblpermision");
            CheckBox ckball = (CheckBox)e.Row.FindControl("ckall");
            HiddenField hdfMenuId = (HiddenField)e.Row.FindControl("hdfMenuId");
            HiddenField hdfPermisionNew = (HiddenField)e.Row.FindControl("hdfPermisionNew");
            HiddenField hdfPermisionSelect = (HiddenField)e.Row.FindControl("hdfPermisionSelect");
            HiddenField hdfPermisionEdit = (HiddenField)e.Row.FindControl("hdfPermisionEdit");
            HiddenField hdfPermisionDelete = (HiddenField)e.Row.FindControl("hdfPermisionDelete");
            if (ckball != null)
            {
                if (edit == 1)
                    ckball.Enabled = true;
                else
                    ckball.Enabled = false;
            }
            if (ckblp != null)
            {
                if (edit == 1)
                {
                    ckblp.Enabled = true;
                    if (CoreTools.CheckDataSet(ds))
                    {
                        ckblp.DataSource = ds;
                        ckblp.DataTextField = "Name";
                        ckblp.DataValueField = "PermisionId";
                        ckblp.DataBind();

                        int count = ds.Tables[0].Rows.Count;
                        if (hdfMenuId != null)
                            for (int j = 0; j < count; j++)
                            {
                                if (GroupId == 1)
                                    ckblp.Items[j].Selected = true;
                                else
                                {
                                    ckblp.Items[j].Selected = false;
                                    string permisinName = ckblp.Items[j].Text;
                                    switch (permisinName)
                                    {
                                        case "新增":
                                            if (hdfPermisionNew != null && hdfPermisionNew.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;
                                        case "修改":
                                            if (hdfPermisionEdit != null && hdfPermisionEdit.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;
                                        case "查看":
                                            if (hdfPermisionSelect != null && hdfPermisionSelect.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;
                                        case "删除":
                                            if (hdfPermisionDelete != null && hdfPermisionDelete.Value == "1")
                                                ckblp.Items[j].Selected = true;
                                            break;

                                    }
                                }
                            }
                    }
                    else
                    {
                        ckblp.Enabled = false;
                    }
                }

            }

        }
    }

    protected void ckall_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;
        DataControlFieldCell dcf = (DataControlFieldCell)chk.Parent;
        GridViewRow gvr = (GridViewRow)dcf.Parent;
        CheckBoxList ckblp = (CheckBoxList)dcf.FindControl("ckblpermision");
        try
        {
            if (chk.Checked)
            {

                if (ckblp != null)
                {
                    for (int i = 0; i < ckblp.Items.Count; i++)
                    {
                        ckblp.Items[i].Selected = true;
                    }
                }
            }
            else
            {

                if (ckblp != null)
                {
                    for (int i = 0; i < ckblp.Items.Count; i++)
                    {
                        ckblp.Items[i].Selected = false;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //throw(this, ex.ToString());
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        UserType ut = new UserType();//用户自定义类型
        DataTable GroupPermisionTable = ut.GetGroupPermisionTableType();
        DataRow GroupPermisionRow;
       
        for (int i = 0; i < grdvPermision.Rows.Count; i++)
        {
            CheckBoxList ckblp = (CheckBoxList)grdvPermision.Rows[i].FindControl("ckblpermision");
            HiddenField hdfMenuId = (HiddenField)grdvPermision.Rows[i].FindControl("hdfMenuId");

            if (ckblp != null && hdfMenuId != null)
            {
                for (int j = 0; j < ckblp.Items.Count; j++)
                {

                    if (ckblp.Items[j].Selected == true)
                    {
                        GroupPermisionRow = GroupPermisionTable.NewRow();
                        GroupPermisionRow["MenuId"] = hdfMenuId.Value;
                        GroupPermisionRow["PermisionId"] = ckblp.Items[j].Value;
                        GroupPermisionTable.Rows.Add(GroupPermisionRow);
                    }
                }
            }

        }
        GroupPermisionTable.AcceptChanges();
        int flag = jtDataAccess.SetGroupPermision(GroupId, GroupPermisionTable);

        switch (flag)
        {

            case 0:
               // CoreTools.JsAlert("操作失败！");
                ScriptManager.RegisterStartupScript(UpdatePanelAccount, this.GetType(), "alert", CoreTools.JsAlert("操作失败,请确认基本信息已保存！"), false);
                break;
            default:
               // CoreTools.JsAlert("操作成功！");
                BindGroupPermision();
                ScriptManager.RegisterStartupScript(UpdatePanelAccount, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                //JsHelper.Alert("操作成功");
                break;
        }
    }
    protected void chkSelect_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <=grdvPermision.Rows.Count - 1; i++)
        {
            CheckBoxList ckblp = (CheckBoxList)grdvPermision.Rows[i].FindControl("ckblpermision");
            if (chkSelect.Checked == true)
            {
                if (ckblp != null)
                {
                    for (int j = 0; j < ckblp.Items.Count; j++)
                    {
                        ckblp.Items[j].Selected = true;
                    }
                }
            }
            else
            {
                if (ckblp != null)
                {
                    for (int j = 0; j < ckblp.Items.Count; j++)
                    {
                        ckblp.Items[j].Selected = false;
                    }
                }
            }
        }
    }
}